package com.aiti.lulian.controller;

import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.ProductInfoDto;
import com.aiti.lulian.dto.ProductNewInfoDto;
import com.aiti.lulian.entity.Product;
import com.aiti.lulian.entity.ProductNew;
import com.aiti.lulian.service.IProductNewService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.apiguardian.api.API;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * description:
 * auth fcb
 * date 2025-04-16 10:30
 **/
@RestController
@RequestMapping("productNew")
public class ProductNewController extends WebController {

    @Resource
    private IProductNewService productService;

    @PostMapping("/addProduct")
    public Message addProduct(@RequestBody ProductNewInfoDto dto) {
        String userId = findUserId();
        try {
            productService.addProduct(dto, userId);
        } catch (Exception e) {
            return Message.fail(e.getMessage());
        }
        return Message.success();
    }

    @GetMapping("/queryProductList")
    public Message queryProductList(ProductNewInfoDto dto) {

        String userId = findUserId();

        Integer pageSize = dto.getPageSize();
        Integer pageNum = dto.getPageNum();
        Page<ProductNew> page = startPage(pageNum, pageSize);

        return Message.success(productService.queryProductList(page, dto, userId));
    }

    @GetMapping("/getProductDetails")
    public Message getProductDetailsBaseInfoNew(@RequestParam("productId") String productId){
        String userId = findUserId();
        return Success(productService.queryNewProductDetail(userId,productId));
    }

    @GetMapping("/queryRandomProductList")
    public Message queryRandomProductList(@RequestParam(value = "num", required = false) Integer num){
        return Success(productService.queryRandomProduct(num));
    }

    @PutMapping("/updateProductById")
    public Message updateProductById(@RequestBody ProductNewInfoDto dto){
        String userId = findUserId();
        productService.updateProductById(dto,userId);
        return Message.success();
    }

    @DeleteMapping("/deleteProductById")
    public Message deleteProductById(@RequestParam("productId")String productId){
        productService.deleteByProductId(productId);
        return Message.success();
    }
}
